﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Engine.RuleEditor.Properties;
using Engine.Rule;
using Engine.Serialization;
using System.IO;

namespace Engine.RuleEditor
{
    public partial class FormRulesEditor : Form
    {
        private RuleSystem rules;
        private string path;
        private string name;

        public FormRulesEditor()
        {
            InitializeComponent();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e); 
            
            FormRulesWizard wizard = new FormRulesWizard();
            if (wizard.ShowDialog() != DialogResult.OK)
            {
                Close();
            }

            switch (wizard.ActionType)
            {
                case FormRulesWizard.ActionTypes.Create:
                    {
                        rules = new RuleSystem();

                        textBoxRulesPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                        textBoxRulesName.Text = "Default";

                        textBoxRulesName.Enabled = true;
                        textBoxRulesPath.Enabled = true;
                    }
                    break;
                case FormRulesWizard.ActionTypes.Open:
                    {
                        rules = readFromFile();

                        textBoxRulesPath.Text = path;
                        textBoxRulesName.Text = name;

                        textBoxRulesName.Enabled = false;
                        textBoxRulesPath.Enabled = false;
                    }
                    break;
                default:
                    break;
            }
        }

        private void buttonAbilityTypesEditor_Click(object sender, EventArgs e)
        {
            FormAbilityTypesEditor form = new FormAbilityTypesEditor(rules.AbilityTypes);
            if (form.ShowDialog() == DialogResult.OK)
            {
                rules.AbilityTypes = form.AbilityTypes;
            }
        }

        private void buttonStatTypesEditor_Click(object sender, EventArgs e)
        {
            FormStatTypesEditor form = new FormStatTypesEditor(rules.StatTypes);
            if (form.ShowDialog() == DialogResult.OK)
            {
                rules.StatTypes = form.StatTypes;
            }
        }

        private void buttonSkillTypesEditor_Click(object sender, EventArgs e)
        {
            FormSkillTypesEditor form = new FormSkillTypesEditor(rules.SkillTypes);
            if (form.ShowDialog() == DialogResult.OK)
            {
                rules.SkillTypes = form.SkillTypes;
            }
        }

        private void buttonTraitTypesEditor_Click(object sender, EventArgs e)
        {

        }

        private void buttonBuffEditor_Click(object sender, EventArgs e)
        {

        }

        private void buttonTraitsEditor_Click(object sender, EventArgs e)
        {

        }

        private void buttonCareerTypesEditor_Click(object sender, EventArgs e)
        {

        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            writeToFile(path, name, rules);
        }

        private void textBoxRulesName_TextChanged(object sender, EventArgs e)
        {
            name = textBoxRulesName.Text;
        }

        private void textBoxRulesPath_TextChanged(object sender, EventArgs e)
        {
            path = textBoxRulesPath.Text;
        }

        private RuleSystem readFromFile()
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FileInfo file = new FileInfo(openFileDialog.FileName);
                path = file.Directory.FullName;
                name = file.Name.Substring(0, file.Name.Length - 4);

                string fileName = path + @"\" + name + ".sre";
                if (File.Exists(fileName))
                {
                    StreamReader dataReader = File.OpenText(fileName);
                    string data = dataReader.ReadToEnd();
                    dataReader.Close();
                    return XMLSerializable<RuleSystem>.Deserialize(data);
                }
            }
            return null;
        }

        private bool writeToFile(string path, string name, RuleSystem rules)
        {
            string fileName = path + @"\" + name + ".sre";
            if (Directory.Exists(path) && !String.IsNullOrEmpty(name))
            {
                string data = XMLSerializable<RuleSystem>.Serialize(rules);
                StreamWriter dataWriter = File.CreateText(fileName);
                dataWriter.Write(data);
                dataWriter.Close();
                return true;
            }
            return false;
        }
    }
}
